#include "ServerScriptEngine.hpp"

ServerScriptEngine::ServerScriptEngine(){
    loadScript("ServerScripts/server.lua");
}

void ServerScriptEngine::startup(Scriptable* server){
    loadFunction("startup");
    server->bind(luaState);
    pushScriptable(server);
    run(1, 0);
}

bool ServerScriptEngine::connectionRequest(const char* addr){
    loadFunction("connection_request");
    pushString(addr);
    if(run(1, 1)){
       return getBool();
    }
    return false;
}

void ServerScriptEngine::clientConnected(Scriptable* client){
    loadFunction("client_connected");
    pushScriptable(client);
    run(1, 0);
}

void ServerScriptEngine::clientTimeout(Scriptable* client){
    loadFunction("client_timeout");
    pushScriptable(client);
    run(1, 0);
}

void ServerScriptEngine::update(unsigned int time){
    loadFunction("update");
    lua_pushinteger(luaState, time);
    run(1, 0);
}

bool ServerScriptEngine::chatMessageReceived(const char* message, Scriptable* client){
    loadFunction("chat_message_received");
    pushString(message);
    pushScriptable(client);
    if(run(2, 1)){
        return getBool();
    }
    return false;
}

void ServerScriptEngine::shutdown(){
    loadAndRun("shutdown");
}